<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.12.
   家居积分更新函数($$)*/

!defined('Q_PATH') && exit;

class Jiaju_Credit{

	public static function RUN($nCpId,$nTbId,$nTbTime,$nType=1,$nCredit=0,$sTips='',$sModuleName=MODULE_NAME){
		if(empty($nCpId) || empty($nTbId) || empty($nTbTime) || empty($sModuleName)){
			return false;
		}

		if(!in_array($sModuleName,array('jiajuevent','jiajupackage','jiajucompanycommentreply','jiajucompanyguestbookreply','jiajucase','jiajudiary','jiajuanli','jiajuvideo','jiajubuild','jiajudesigner','jiajuorder','jiajubuildorder','jiajucompanycomment','jiajucompanyguestbook'))){
			return false;
		}

		// 获取积分配置
		$arrJiajucreditconfig=Model::F_('jiajucreditconfig','jiajucreditconfig_module=? AND jiajucreditconfig_status=1',$sModuleName)
			->getOne();
		if(empty($arrJiajucreditconfig['jiajucreditconfig_id'])){
			return;
		}

		// 获取商家信息
		$arrJiajucompany=Model::F_('jiajucompany','jiajucompany_id=? AND jiajucompany_status=1',$nCpId)
			->getOne();
		if(empty($arrJiajucompany['jiajucompany_id'])){
			return;
		}

		$nCredit=!empty($nCredit)?$nCredit:($nType==0?$arrJiajucreditconfig['jiajucreditconfig_cut']:$arrJiajucreditconfig['jiajucreditconfig_add']); // 默认为配置的大小
		$sTips=!empty($sTips)?$sTips:($nType==0?$arrJiajucreditconfig['jiajucreditconfig_cuttips']:$arrJiajucreditconfig['jiajucreditconfig_addtips']); // 添加的提示内容
		$sTips=str_replace('{num}',$nCredit,$sTips);

		if($nCredit<=0){// 0分不在进行设置和记录
			return true;
		}

		// 积分日志
		$arrLogData=array(
			'jiajucreditlog_grade'=>$nCredit,
			'jiajucompany_id'=>$nCpId,
			'jiajucreditlog_module'=>$sModuleName,
			'jiajucreditlog_itemid'=>$nTbId,
			'jiajucreditlog_itemtime'=>$nTbTime,
			'jiajucreditlog_tips'=>$sTips,
		);

		// 减少积分
		if($nType==0){
			$nCurrentCredit=$arrJiajucompany['jiajucompany_credit']-$nCredit;
			$nCurrentCredit<0 && $nCurrentCredit=0;
			if($nCurrentCredit==$arrJiajucompany['jiajucompany_credit']){
				return;
			}

			// 判断是否已经记录过日志
			$arrTryLog=Model::F_('jiajucreditlog','jiajucreditlog_type=0 AND jiajucreditlog_itemid=? AND jiajucreditlog_module=?',$nTbId,$sModuleName)
				->getOne();
			if(!empty($arrTryLog['jiajucreditlog_id'])){
				return;
			}
			
			Model::M_('jiajucompany')->updateWhere(array('jiajucompany_credit'=>$nCurrentCredit),'jiajucompany_id=?',$nCpId);

			$arrLogData['jiajucreditlog_type']=0;
			$oTryLog=new JiajucreditlogModel($arrLogData);
			$oTryLog->save();
			return;
		}

		// 验证每天增加的积分上限
		if($arrJiajucreditconfig['jiajucreditconfig_daycount']>0){
			$nDaystarttime=strtotime(date('Y-m-d',$nTbTime)); // 发布时间当天的开始时间戳
			$nDayendtime=strtotime(date('Y-m-d',$nTbTime).' 23:59:59'); // 发布时间当天的结束时间戳
			
			$arrWhere=array();
			$arrWhere['jiajucreditlog_itemtime']=array('between',array($nDaystarttime,$nDayendtime));
			$arrWhere['jiajucompany_id']=$nCpId; // 商铺编号
			$arrWhere['jiajucreditlog_module']=$sModuleName; // 相关模块
			$arrWhere['jiajucreditlog_type']=1;

			$nDaycount=Model::F_('jiajucreditlog')
				->where($arrWhere)
				->getSum('jiajucreditlog_grade');
			$nDaycount=$nDaycount<0?0:$nDaycount;

			if($nDaycount>=$arrJiajucreditconfig['jiajucreditconfig_daycount']){// 达到积分上限
				// 积分上限的提示是否已经添加
				$arrWhere['jiajucreditlog_type']=2;
				$arrTryLog=Model::F_('jiajucreditlog')
					->where($arrWhere)
					->getOne();
				if(!empty($arrTryLog['jiajucreditlog_id'])){
					return;
				}

				// 添加积分达到上限的提示
				$arrJiajucreditconfig['jiajucreditconfig_counttips']=str_replace('{day}','['.date('Y-m-d',$nTbTime).'] ',$arrJiajucreditconfig['jiajucreditconfig_counttips']);
				$arrLogData['jiajucreditlog_type']=2;
				$arrLogData['jiajucreditlog_grade']=0;
				$arrLogData['jiajucreditlog_tips']=$arrJiajucreditconfig['jiajucreditconfig_counttips'];

				$oTryLog=new JiajucreditlogModel($arrLogData);
				$oTryLog->save();
				return true;
			}
		}

		// 验证每月的次数
		if($arrJiajucreditconfig['jiajucreditconfig_monthcount']!=0){
			$nMonthstarttime=strtotime(date('Y-m-01',$nTbTime)); //发布时间的当月开始时间戳
			$nMonthendtime=strtotime(date('Y-m-01',$nTbTime).' +1 month -1 day 23:59:59'); // 发布时间当月的结束时间戳

			$arrWhere=array();
			$arrWhere['jiajucreditlog_itemtime']=array('between',array($nMonthstarttime,$nMonthendtime)); // 当月
			$arrWhere['jiajucompany_id']=$nCpId; //商铺编号
			$arrWhere['jiajucreditlog_module']=$sModuleName; //相关模块
			$arrWhere['jiajucreditlog_type']=1;

			$nMonthcount=Model::F_('jiajucreditlog')
				->where($arrWhere)
				->getCounts();
			$nMonthcount=$nMonthcount<0?0:$nMonthcount;

			if($nMonthcount>=$arrJiajucreditconfig['jiajucreditconfig_monthcount']){
				// 添加没有积分次数的提示
				$arrWhere['jiajucreditlog_type']=3;
				$arrTryLog=Model::F_('jiajucreditlog')
					->where($arrWhere)
					->getOne();
				if(!empty($arrTryLog['jiajucreditlog_id'])){
					return;
				}

				// 添加积分达到上限的提示
				$arrJiajucreditconfig['jiajucreditconfig_monthtips']=str_replace('{month}','['.date('Y-m',$nTbTime).'] ',$arrJiajucreditconfig['jiajucreditconfig_monthtips']);
				$arrLogData['jiajucreditlog_type']=3;
				$arrLogData['jiajucreditlog_grade']=0;
				$arrLogData['jiajucreditlog_tips']=$arrJiajucreditconfig['jiajucreditconfig_monthtips'];

				$oTryLog=new JiajucreditlogModel($arrLogData);
				$oTryLog->save();
				return true;
			}
		}

		// 判断是否已经记录过日志
		$arrTryLog=Model::F_('jiajucreditlog','jiajucreditlog_type=1 AND jiajucreditlog_itemid=? AND jiajucreditlog_module=?',$nTbId,$sModuleName)
			->getOne();
		if(!empty($arrTryLog['jiajucreditlog_id'])){
			return;
		}

		// 修改商家积分
		Model::M_('jiajucompany')->updateWhere(array('jiajucompany_credit'=>$arrJiajucompany['jiajucompany_credit']+$nCredit),'jiajucompany_id=?',$nCpId);

		$arrLogData['jiajucreditlog_type']=1;
		$oTryLog=new JiajucreditlogModel($arrLogData);
		$oTryLog->save();
		return;
	}

}
